package com.opennews.openplatform.jooq;

import com.opennews.openplatform.myspringbootcore.util.DateHelper;
import org.jooq.Field;
import org.jooq.Record;

import java.time.LocalDateTime;
import java.util.UUID;

public class RecordHelper {
    public static void setCommonFields(Record record) {
        setIdValue(record);
        setCreatedAtValue(record);
        setUpdatedAtValue(record);
    }

    public static void setUpdateFields(Record record) {
        setUpdatedAtValue(record);
    }

    static private void setIdValue(Record record) {
        Field<String> field = record.field("id", String.class);

        if (field != null) {
            record.set(field, UUID.randomUUID().toString().replaceAll("-", ""));
        }
    }

    static private void setCreatedAtValue(Record record) {
        Field<LocalDateTime> field = record.field("date_created", LocalDateTime.class);

        if (field != null) {
            record.set(field, DateHelper.getCurrentLocalDateTime());
        }
    }

    static private void setUpdatedAtValue(Record record) {
        Field<LocalDateTime> field = record.field("last_updated", LocalDateTime.class);

        if (field != null) {
            record.set(field, DateHelper.getCurrentLocalDateTime());
        }
    }
}
